package smlab.effects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
		
	/**
	 * 镜像(无渐变倒影)
	 * @author Administrator
	 * @author DN
	 */
	public class Reflection_SM extends Sprite
	{
		private var _img:Bitmap;
		private var _distance:Number;
		private var _offset:Number;
		private var _reflectionImgData:BitmapData
		private var _reflectionImg:Bitmap;
		
		private var _reflectionContainer:Sprite;
		private var _reflectionLight:Sprite;
		/**
		 * 构造函数
		 * @param bm   倒影图片对象
		 * @param distance 倒影与图片距离
		 * @param offset   倒影偏移量
		 * 此类生成原显示对象的副本
		 */
		public function Reflection_SM(bm:Bitmap,distance:Number=0,offset:Number=100) 
		{
			_img = bm;
			_distance = distance;
			_offset = offset;
			
			addChild(_img);
			initReflection();
		}
		
		private function initReflection():void{
			var imgData:BitmapData = _img.bitmapData;
			_reflectionImgData = imgData.clone();
			_reflectionImg = new Bitmap(_reflectionImgData);
			
			_reflectionContainer = new Sprite();
			_reflectionImg.scaleY *= -1;
			_reflectionImg.y = _img.y + (_img.height*2) + _distance;
			_reflectionContainer.addChild(_reflectionImg);
			_reflectionContainer.blendMode = BlendMode.LAYER;
			
			_reflectionLight = new Sprite();
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x000000, 0xFFFFFF];
			var alphas:Array = [1, 0];
			var ratios:Array = [0, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(_img.width,_img.height , Math.PI / 2, 0, _offset+_distance);
			var spreadMethod:String = SpreadMethod.PAD;
			_reflectionLight.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
			_reflectionLight.graphics.drawRect(0, _img.y+_img.height+_distance, _img.width, _img.height);
			_reflectionLight.graphics.endFill();
			_reflectionLight.blendMode = BlendMode.ALPHA;
			_reflectionContainer.addChild(_reflectionLight);
			this.addChild(_reflectionContainer);
		}
		
		/**
		 * 直接倒影方法
		 * (在原显示对象里直接生成倒影，而不生成副本)
		 * @param d				
		 * @param distanceY	倒影与显示对象Y轴距离
		 * @param distanceX	倒影与显示对象X轴距离
		 */
		public static function createReflection(d:DisplayObject,distanceY:Number=0,distanceX:Number=0):void{
			//对源显示对象做上下反转处理
			var bd:BitmapData=new BitmapData(d.width,d.height,true,0);
			var mtx:Matrix=new Matrix();
			mtx.d=-1;mtx.ty=bd.height;
			bd.draw(d,mtx);
			//生成一个渐变遮罩
			var width:int=bd.width;
			var height:int=bd.height;
			mtx=new Matrix();
			mtx.createGradientBox(width,height,0.5 * Math.PI);
			var shape:Shape = new Shape();
			shape.graphics.beginGradientFill(GradientType.LINEAR,[0,0],[0.9,0.2],[0,0xFF],mtx)
			shape.graphics.drawRect(0,0,width,height);
			shape.graphics.endFill();
			var mask_bd:BitmapData=new BitmapData(width,height,true,0);
			mask_bd.draw(shape);
			//生成最终效果
			bd.copyPixels(bd,bd.rect,new Point(0,0),mask_bd,new Point(0,0),false);
			//将倒影位图放在源显示对象下面
			var ref:Bitmap=new Bitmap();
			ref.y=d.height+distanceY;
			ref.x=distanceX;
			ref.bitmapData=bd;
			d.parent.addChild(ref);
		}
	}
}